package com.hnust.service.impl;

import com.hnust.dao.CollegeDao;
import com.hnust.dao.GradRequirementsDAO;
import com.hnust.dao.MajorDao;
import com.hnust.domain.GradRequirement;
import com.hnust.domain.Major;
import com.hnust.domain.record.MajorGradRequirement;
import com.hnust.domain.record.MajorListRecord;
import com.hnust.domain.record.MajorListResult;
import com.hnust.service.MajorService;
import com.hnust.util.IDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service("majorService")
public class MajorServiceImpl implements MajorService {

    @Autowired
    private MajorDao majorDao;
    @Autowired
    private CollegeDao collegeDao;
    @Autowired
    private GradRequirementsDAO gradRequirementsDAO;
    @Override
    public MajorListResult getMajorList(String collegeId,int current,int rows,String majorName) {
        // 返回值
        MajorListResult majorListResult = new MajorListResult();
        // 获取学院的名字
        String name = collegeDao.findNameByCollegeId(collegeId);

        List<Major> data;
        if(majorName.equals("")){
            data = majorDao.getMajorListByCollegeId(collegeId,rows,current*rows);
        }else{
            data=majorDao.getMajorListByCollegeIdAndMajorName(collegeId,rows,current*rows,majorName);
        }
        List<MajorListRecord> list = new ArrayList<>();
        // 遍历查找到的专业列表
        for(Major major:data){
           MajorListRecord majorRecord = new MajorListRecord();
           majorRecord.setMajorId(major.getMajId());
           majorRecord.setName(major.getName());
           majorRecord.setCollegeName(name);
           list.add(majorRecord);
        }
        majorListResult.setMajorRecord(list);
        majorListResult.setCount(data.size());
        //System.out.println(majorListResult);
        return majorListResult;
    }

    // 删除专业
    @Override
    public boolean deleteMajor(List<String> ids) {
        // 我们直接删除，然后判断是否全部删除
        return majorDao.deleteMajorByMajorId(String.join(",", ids)) == ids.size();
    }

    // 添加专业
    @Override
    public MajorListRecord addMajor(String name, String collegeId) {
        // 生成UUID
        String id = IDUtil.getPrimaryKey();
        // 获取学院的名字
        String collegeName = collegeDao.findNameByCollegeId(collegeId);
        MajorListRecord record = new MajorListRecord();
        if (majorDao.addMajor(id,name,collegeId) == 1){
            record.setMajorId(id);
            record.setName(name);
            record.setCollegeName(collegeName);
        }
        return record;
    }

    // 修改专业
    @Override
    public boolean changeMajor(String majorName, String majorId) {
        return majorDao.updateMajorName(majorId,majorName) == 1;
    }

    @Override
    public MajorGradRequirement getMajorGradRequirement(String majorId) {
        MajorGradRequirement gradRequirement=new MajorGradRequirement();
        Major major=majorDao.getMajor(majorId);
        BeanUtils.copyProperties(major,gradRequirement);
        List<GradRequirement> requirements=gradRequirementsDAO.getGradRequirement(majorId);
        requirements.sort(Comparator.comparing(GradRequirement::getNum));
        gradRequirement.setGradRequirements(requirements);
        System.out.println(gradRequirement);
        return gradRequirement;
    }
}
